﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cls.u_Models
{
    public partial class IssusepluMgr : System.Web.UI.Page
    {
        static String UserName = "";
        static String UserInOrgCode = "";
        static String UserInOrgName = "";
        static Boolean UserInZB = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            UserName = User.Identity.Name;

            if (UserName.Equals(""))
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                UserInOrgCode = GetOrgCodeByUserName(UserName);
                UserInOrgName = GetOrgNameByUserName(UserName);
            }

            if (!Page.IsPostBack)
            { }

            Label_UserName.Text = UserName;
            Label_OrgName.Text = UserInOrgCode + "-" + UserInOrgName;

            if (UserInOrgCode.Trim().Equals("ZB"))
            {
                Label_NavLink.Text = "<ol>";
                //Label_NavLink.Text += "<li><a href=\"../u_ExcelFiles/门店异常商品表（模版）.xlsx\">下载《门店异常商品表（模版）》</a></li>";
                //Label_NavLink.Text += "<li><a href=\"FileUpload.aspx?App=Issuseplu\">上传门店异常商品表</a></li>";
                Label_NavLink.Text += "<li><a href=\"ShowIssuseplu.aspx?OrgCode=ZB\">查看门店异常商品电子表单</a></li>";
                //Label_NavLink.Text += "<li><a href=\"..\\Default.aspx\">返回主页</a></li>";
                //Label_NavLink.Text += "<li><a href=\"BackorderUserMgr.aspx\">用户管理</a></li>";
                Label_NavLink.Text += "</ol>";
            }
            else
            {
                Label_NavLink.Text = "<ol>";
                Label_NavLink.Text += "<li><a href=\"../u_ExcelFiles/门店异常商品上报表（模板）.xlsx\">下载《门店异常商品上报表（模板）》</a></li>";
                Label_NavLink.Text += "<li><a href=\"FileUpload.aspx?App=Issuseplu&OrgCode=" + UserInOrgCode + "\">上传门店异常商品表</a></li>";
                Label_NavLink.Text += "<li><a href=\"ShowIssuseplu.aspx?OrgCode=" + UserInOrgCode + "\">查看门店异常商品电子表单</a></li>";
                Label_NavLink.Text += "</ol>";
            }
        }

        protected String GetOrgCodeByUserName(String UserName)
        {
            String OrgCode = "";

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            SqlCommand SelectCommand = new SqlCommand("SELECT OrgCode FROM u_cls_Org WHERE Id = (SELECT OrgId FROM u_cls_UserInOrg WHERE UserName = @UserName)", SqlConnection);

            SelectCommand.Parameters.Add("@UserName", SqlDbType.NVarChar, 4);
            SelectCommand.Parameters["@UserName"].Value = UserName;

            SqlDataReader OrgCodeReader = SelectCommand.ExecuteReader();

            while (OrgCodeReader.Read())
            {
                //还是分开取得比较好，后面还有单独用到OrgCode的地方......
                //OrgCode = ResReader["OrgCode"].ToString() + "-" + ResReader["OrgName"].ToString();  
                OrgCode = OrgCodeReader["OrgCode"].ToString();
            }

            if (OrgCode.Equals("ZB"))
            {
                UserInZB = true;
            }

            OrgCodeReader.Close();
            SqlConnection.Close();

            return OrgCode;
        }

        protected String GetOrgNameByUserName(String UserName)
        {
            String OrgName = "";

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            SqlCommand SelectCommand = new SqlCommand("SELECT OrgName FROM u_cls_Org WHERE Id = (SELECT OrgId FROM u_cls_UserInOrg WHERE UserName = @UserName)", SqlConnection);

            SelectCommand.Parameters.Add("@UserName", SqlDbType.NVarChar, 4);
            SelectCommand.Parameters["@UserName"].Value = UserName;

            SqlDataReader OrgNameReader = SelectCommand.ExecuteReader();

            while (OrgNameReader.Read())
            {
                //还是分开取得比较好，后面还有单独用到OrgCode的地方......
                //OrgCode = ResReader["OrgCode"].ToString() + "-" + ResReader["OrgName"].ToString();  
                OrgName = OrgNameReader["OrgName"].ToString();
            }

            OrgNameReader.Close();
            SqlConnection.Close();

            return OrgName;
        }
    }
}